/**
 * 基本药物弹窗模块，点击图表首页后初始化弹窗数据
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","valueUtil","panelUtil","btnServ","datagridServ","commonServ","formServ","overlayInit"],
		function($,sysmanMain,valueUtil,panelUtil,btnServ,datagridServ,commonServ,formServ,overlayInit) {

		var rootid = 'panel-overlay-prescribe-pop';
		
		var window_id = 'overlay-prescribe-pop-win';
		
		var panel_hidden_class = 'panel-outpatient-hidden';
		
		var module_code ='overlayPrescribeInfo';
		var module_code_head = 'overlayPrescribe';
		var queryParam ;
		
		
		//类型定义
		var layerConfig = {
			init		: 'init',	//全院
			dept		: 'dept',		//科室
			doct		: 'doct',		//医生
			patient 	: 'patient',	//患者
			prescribe	: 'prescribe'	//处方层
		}	
		
		var defaultLayer = {
			layer	: layerConfig.dept,
			dept_code	: null, //当前科室id
			doct_code	: null,	//当前医生
			mzhm	: null //当前门诊患者记录id
		}
		
		//当前查看信息
		var currentLayer = {
			dept_code	: null, //当前科室id
			
			doct_code	: null,	//当前医生
			
			mzhm	: null, //当前门诊患者记录id					
			
			layer		: layerConfig.dept		//当前层级
		}		
		
		
		//格式化函数
		var formatValue=function(value,row,index){
			if (value > 0){
				return   (value*100).toFixed(2) +'%';							
			}else{
				return '0.00%';
			}
		}
		
		
		var patientUrl = '/overlayPrescribe/query/getOutpatientListDomain.action';
		
		var public_method ={
			
			openPage:function(_options){
				
				var config = {
					title	: '超限处方',
					href	: '/'+sysmanMain.getRootPath()+'/' + module_code_head + '/query/popPage.action',
					window	: $('#' + window_id),
					maximized : true,
					resizable : false,
					flag	: false,					
					onClose : private_method.closePage
				}
				queryParam = _options;
				var _param = $.extend({},config,_options)
				datagridServ.windowEditCenter(_param);
			}
			,
			init:function(_options){
				//清除缓存
				currentLayer = $.extend({},defaultLayer);			
				
				private_method.initdate();
				var _queryParams = $.extend({},private_method.getCondition())
				
				overlayInit.init({
					rootid	: rootid,
					condition	: _queryParams,
					cellClick	: private_method.clickDeptCell
				})
				//获取查询参数			
				private_method.breadcrumb({name:'开始',layer:'init'})
			}
			
			,
			/**
			 * 重建grid
			 * @param _options.layer	重建grid类型
			 */
			recreateGrid:function(_options){				
				
				var _grid = $('#'+rootid + ' .dg-list')
				var _param = {
					url			: '/'+sysmanMain.getRootPath()+ url,
					rownumbers  : true,
					animate		: true,						
					striped		: true,
					border		: true,					
					idField		: 'id',						
					fitColumns	: true,
					
					onClickCell : private_method.editCell
				}
				
				var _patientParam
				
				if(currentLayer.layer){
					//隐藏所有数据
					private_method.hiddenList();
					switch(currentLayer.layer){
						case layerConfig.dept.layer:
						
						_grid = $('#'+rootid +  ' .' + layerConfig.dept.grid);
							_param.columns = columns_config.deptColumns;
							
							//清理数据
							currentLayer.dept_code = null;
							currentLayer.doct_code = null;
							currentLayer.mzhm = null;
							var _queryParams = $.extend(
								{									
									layer		: currentLayer.layer
								},
								private_method.getCondition())
							_param.queryParams={condition:_queryParams};
							
							break;
						case layerConfig.doct.layer :
						
							_grid = $('#'+rootid +  ' .' + layerConfig.doct.grid);
							_param.columns = columns_config.doctColumns;
							
							//清理数据
							currentLayer.doct_code = null;
							currentLayer.mzhm = null;	
							
							var _queryParams = $.extend(
								{
									dept_code	: currentLayer.dept_code ,
									layer		: currentLayer.layer
								},
								private_method.getCondition())
							_param.queryParams={condition:_queryParams};
							break;
						case layerConfig.patient.layer:
							
							_grid = $('#'+rootid +  ' .' + layerConfig.patient.grid);
							_patientParam = {
								url			: '/'+sysmanMain.getRootPath()+ patientUrl,
								rownumbers  : true,
								animate		: true,						
								striped		: true,
								border		: true,					
								idField		: 'id',						
								fitColumns	: true,								
								onClickCell : private_method.editCell
							}
							
							var _condition = private_method.getCondition()
							_patientParam.queryParams={condition : _condition};
							_patientParam.columns = columns_config.patientColumns;
							//不再响应表格点击事件							
							break;
						case layerConfig.init.layer:
							currentLayer = $.extend({},defaultLayer);
						default:
							_param.columns = columns_config.deptColumns;
							break;
					}
				}
				
				
				
				
				_grid.datagrid(_param);
				
				private_method.showList({
					grid:layerConfig[currentLayer.layer].grid
				})
				
				//绑定查询事件
				private_method.bindBtnEvent();
			}
			
		}
		
		var private_method ={
			/**
			 * 点击某层数据后的响应函数
			 * @param _options.row	点击行数据
			 * @param _options.field	点击字段名称
			 * @param _options.value	点击字段值
			 * @param _options.filter	需保存的数据过滤条件
			 * @param _options.layer	当前数据所在层级
			 * @param _options.layerData	当前层级需保存的状态值
			 * @param _options.nextLayer	点击后显示的下一层级数据
			 * @param _options.breadParam	下一层次面包屑参数
			 * 			 
			 * */
			clickDeptCell:function(_options){
				if(window.console){
					console.log(' clickDeptCell called...');
				}		
				
				//点击表格值是否大于0判断
				if( (!_options) || (!_options.value) ||  (_options.value <=0) ){
					
					return;
				}
				
				//保存层级数据状态
				currentLayer = $.extend({},_options.layerData);
							
				
				//面包屑层次增加处理
				var breadParam = _options.breadParam;
				private_method.breadcrumb(breadParam);
				
				//隐藏所有数据视图面板
				private_method.hiddenList();
				
				//显示下一层级面板
				private_method.showList({layer:_options.breadParam.layer})
			},
			//隐藏所有数据控件
			hiddenList:function(){
				var _el = $('#'+ rootid + ' .outpatient-bread-panel')
				if(_el.length>0){
					_el.addClass(panel_hidden_class);
				}
			},
			/**
			 * @param _options.layer	显示指定数据控件的内容
			 */
			showList:function(_options){
				
				var _o = $.extend({},_options,{grid:''});
				var _el = $('#'+ rootid + ' .user-layer-'+_options.layer);
				if(_el.length==1){
					_el.removeClass(panel_hidden_class);
				}
			},
			initdate:function(){
				if(window.console){
					console.log(' init overlay time...');
				}
				var _el = $('#panel-chart-all #monitor_year_month')
				if(_el.length==1){
					
					var _yearMonth = _el.datebox('getValue');
					var nowYear = _yearMonth.substring(0,4);
					var nowMonth = _yearMonth.substring(5,_yearMonth.length); //当前月
					
					
					var monthStartDate = _yearMonth+'-1';
					 
					$('#' + rootid + ' #start_time').datebox('setValue',monthStartDate)					
					
					
					var monthEndDate = commonServ.getMonthLastDay({yearMonth:_yearMonth});
					$('#' + rootid + ' #end_time').datebox('setValue',monthEndDate)
					
				}
				
			},
			
			
			/**
			 * 点击表格单元响应函数
			 * @param _options.row	点击行数据
			 * @param _options.field	点击列名称
			 * @param _options.value	点击表格数据
			 * @param _options.layer	点击所在层
			 */
			editCell:function(row,field,value,layer){
				
				if(window.console)
					console.info('overlayPrescribe editCell fired ...')			
				
				var breadParam = {}
			
				if(row){
					var checked = false;
					if(!checked && (layer == layerConfig.patient)){					
						//患者层
						currentLayer.mzhm = row.mzhm;
						currentLayer.layer = layerConfig.prescribe;
						
						breadParam.name = row.patient_name;
						checked = true;
					}
					
					if(!checked && (layer == layerConfig.doct)){
						//当前在医生层
						currentLayer.doct_code = row.doct_code;
						
						//进入患者层
						currentLayer.layer = layerConfig.patient;
						breadParam.name = row.doct_name;
						checked = true;
						
						//清除数据
						currentLayer.patient_id = null;
					}
					if(!checked && (layer == layerConfig.dept)){
						//当前在科室层
						currentLayer.dept_code = row.dept_code;
						
						//将要进入医生层
						currentLayer.layer = layerConfig.doct;
						breadParam.name = row.dept_name;
						//清除数据
						currentLayer.doct_code = null;
						currentLayer.mzhm = null;							
					}
					
					if(!checked && (layer == layerConfig.init)){
						//当前在初始层
						currentLayer.dept_code = null;
						
						//将要进入科室层
						currentLayer.layer = layerConfig.dept;
						
						breadParam.name = '开始';
						//清除数据
						currentLayer.doct_code = null;
						currentLayer.mzhm = null;							
					}
				
				}
				private_method.breadcrumb(breadParam)
//				public_method.recreateGrid();				
				
			},
			bindBtnEvent:function(){

				var window_btn = $('#'+rootid +' a.function-btn')
				$(window_btn).unbind('click')
				$(window_btn).bind('click',	private_method.function_btn_listenner)

			},			

			// 功能按钮监听函数
			function_btn_listenner : function(e) {
				if(window.console){
					console.log('got a return here~~~');
				}
				btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

			}
			,
			/**
			 * 面包屑增加层级操作
			 * @param _options.name	显示名称
			 * @param _options.layer	层级
			 */
			breadcrumb:function(_options){
				
				var _el = $('#'+rootid + ' .breadcrumb')
				if(_el.length==1){
					var _h = '<li class="bread-item"><a href="#" class="bread-link" btn-type="'+ _options.layer+'">' +_options.name+'</a></li>'
					_el.append(_h);
				}
				private_method.bindBreadEvent();
			}
			,
			/**
			 * 面包屑事件
			 */
			bindBreadEvent:function(){

				var window_btn = $('#'+rootid +' .bread-link')
				$(window_btn).unbind('click')
				$(window_btn).bind('click',	private_method.breadClick)

			},
			
			/**
			 * 面包屑点击事件
			 */
			breadClick:function(e){
				if(e && e.currentTarget){
					var ce = $(e.currentTarget);
					var el = $(e.currentTarget).parent().nextAll();
					if(el.length>0){
						$(el).remove();
					}
					
					//设定当前层级
					var _type = ce.attr('btn-type')
					currentLayer.layer = _type;					
				}
			}
			,			
			getCondition:function(){
				var _form = $('#'+ rootid +' .condition-form ')
				var ret = formServ.getDataObj({
					rootId:rootid,
					form : $(_form)
				});
				
				$.extend(ret,currentLayer);
				return ret;
			}
			,
			//执行查询
			doSearch:function(e){
				var ret = private_method.getCondition();

				var $dg = $('#' + rootid + " .dg-list")
				$($dg).datagrid('load', {condition:ret});
				if(e && e.currentTarget){
					$(e.currentTarget).linkbutton('enable');
				}
			}
			,
			closePage:function(){
				currentLayer = $.extend({},defaultLayer);
				$('#'+window_id).empty();
			}
		}
			
		
		return public_method;
			
})